INSERT INTO  TRASPARENZA

select 'T30',BOLLETTA,'','TOTALE ONERI DIVERSI SOGGETTI IVA','','','','','','','',SUM(TOTALE),'',null FROM(
SELECT '',BOLLETTA,'','','','','','','','','',SUM(TOTALE) totale,'' FROM TRASPARENZA 
WHERE RIGA IN ('T27','T28','T29') AND ALI_IVA IS NOT NULL and ALI_IVA <> 0 GROUP BY BOLLETTA
union 
SELECT '',BOLLETTA,'','','','','','','','','',0 totale,'' FROM TRASPARENZA GROUP BY BOLLETTA
union
SELECT '',BOLLETTA,'','','','','','','','','',CO.TOTALE_I-TOTALE  totale,'' 
FROM TRASPARENZA,TMPMARKI TM,db5.competenze@pgu3 co
WHERE RIGA IN ('T26')
AND TM.CODICE_BOLLETTA=TRASPARENZA.BOLLETTA
AND TM.DIP_ENEL IS NULL
and co.CODICE_GESTIONE='UNRG'
and co.CODICE_COMPETENZA=trasparenza.bolletta
AND co.CODICE_ORIGINE_COMPETENZA='B'
)

GROUP BY BOLLETTA





